<template>
  <div>
       <common-head title="京东商城注册" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="userInfo.username"
        name="username"
        label="用户名/手机"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="userInfo.pwd"
        type="password"
        name="pwd"
        label="请输入密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
    <van-button @click="$router.push('/login')">登录</van-button>
  </div>
</template>

<script>
import CommonHead from "_components/CommonHead"; 
import { doRegister } from "_api";
import { Toast } from "vant";
export default {
  data() {
    return {
      userInfo: {
        username: "",
        pwd: "",
      },
    };
  },
  methods: {
    onSubmit(params) {
      // 注册
      doRegister(params).then((res) => {
        if (res.data.code === 0) {
          // 注册成功 弹出成功提醒 跳转到登录（replace）
          Toast({
            type: "success",
            message: "注册成功",
            duration: 1000,
            onClose: () => {
              // 去登录页
              this.$router.replace("/login");
            },
          });
        } else {
          Toast({
            type: "fail",
            message: "注册失败",
          });
        }
      });
    },
  },
    components:{
    CommonHead
  }
};
</script>

<style lang="scss" scoped>
</style>
